package com.dream.keigezhushou.Activity.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dream.keigezhushou.R;


/**
 * 自定义RatingBar
 * <p>
 * 可自定义样式和间距
 */

public class MyRatingBar extends LinearLayout {

    /**
     * 是否可点击
     */
    private boolean mClickable;

    /**
     * 星星总数
     */
    private int starCount;

    /**
     * 星星的点击事件
     */
    private OnRatingChangeListener onRatingBarChangeListener;

    /**
     * 每个星星的大小
     */
    private float starImageSize;

    /**
     * 星星之间的间距
     */
    private float starPadding;

    /**
     * 空白星星资源文件
     */
    private Drawable starEmptyDrawable;

    /**
     * 选中满星的资源文件
     */
    private Drawable starFillDrawable;

    /**
     * 星星显示的数量，整型
     */
    private int starStep;

    /**
     *  每次点击星星增加的数量
     */
    private StepSize stepSize;

    /**
     * 设置空白的图片资源文件
     *
     * @param starEmptyDrawable
     */
    public void setStarEmptyDrawable(Drawable starEmptyDrawable) {

        this.starEmptyDrawable = starEmptyDrawable;
    }

    /**
     * 设置满星的图片资源文件
     *
     * @param starFillDrawable
     */
    public void setStarFillDrawable(Drawable starFillDrawable) {

        this.starFillDrawable = starFillDrawable;
    }

    /**
     * 设置星星是否可点击操作
     *
     * @param clickable
     */
    public void setClickable(boolean clickable) {

        this.mClickable = clickable;
    }

    /**
     * 设置点击事件
     *
     * @param onRatingBarChangeListener
     */
    public void setOnRatingBarChangeListener(OnRatingChangeListener onRatingBarChangeListener) {

        this.onRatingBarChangeListener = onRatingBarChangeListener;
    }

    /**
     * 设置星星的大小
     *
     * @param starImageSize
     */
    public void setStarImageSize(float starImageSize) {

        this.starImageSize = starImageSize;
    }

    public void setStepSize(StepSize stepSize){
        this.stepSize = stepSize;
    }

    /**
     * 构造函数
     *
     * @param context
     * @param attrs
     */

    public MyRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //设置方向
        setOrientation(LinearLayout.HORIZONTAL);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRatingBar);
        starImageSize = mTypedArray.getDimension(R.styleable.MyRatingBar_starImageSize, 11);
        starPadding = mTypedArray.getDimension(R.styleable.MyRatingBar_starPadding, 12);
        starStep = mTypedArray.getInteger(R.styleable.MyRatingBar_starStep, 1);
        starCount = mTypedArray.getInteger(R.styleable.MyRatingBar_starCount, 5);
        starEmptyDrawable = mTypedArray.getDrawable(R.styleable.MyRatingBar_starEmpty);
        starFillDrawable = mTypedArray.getDrawable(R.styleable.MyRatingBar_starFull);
        mClickable = mTypedArray.getBoolean(R.styleable.MyRatingBar_starClickable, true);
        mTypedArray.recycle();

        for (int i = 0; i < starCount; ++i) {

            final ImageView imageView = getImageView();
            imageView.setImageDrawable(starEmptyDrawable);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    //每次点击增加一颗星
                    if (mClickable){

                        int fint = starStep;
                        if (indexOfChild(view) > fint){
                            setStar(indexOfChild(view) + 1);
                        }
                    }
                }
            });

            addView(imageView);
        }

        setStar(starStep);

    }

    /**
     * 设置每颗星星的参数
     *
     * @return
     */
    private ImageView getImageView() {

        ImageView imageView = new ImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(
                Math.round(starImageSize), Math.round(starImageSize) //设置每颗星星在线性布局中的大小
        );
        layoutParams.setMargins(0, 0, Math.round(starPadding), 0); //设置每颗星星在线性布局中的间距
        imageView.setLayoutParams(layoutParams);
        imageView.setAdjustViewBounds(true);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageDrawable(starEmptyDrawable);
        imageView.setMinimumWidth(10);
        imageView.setMaxHeight(10);
        return imageView;
    }

    /**
     *  设置星星的个数
     * @param rating
     */
    public void setStar(int rating){

        if (onRatingBarChangeListener != null){

            onRatingBarChangeListener.onRatingChange(rating);
        }

        this.starStep = rating;

        int fint = rating;

        //设置选中的星星
        for (int i = 0; i < fint; ++i){

            ((ImageView) getChildAt(i)).setImageDrawable(starFillDrawable);
        }

        //设置没有选中的星星
        for (int i = fint; i < starCount;i++){

            ((ImageView)getChildAt(i)).setImageDrawable(starEmptyDrawable);
        }
    }

    /**
     *
     * 操作星星的点击事件
     *
     */

    public interface OnRatingChangeListener{

        /**
         *  选中星星的个数
         * @param ratingCount
         */
        void onRatingChange(int ratingCount);
    }


    public enum StepSize{

    }
}
